//
//  DZMRMFuncView.swift
//  DZMeBookRead
//
//  Created by 邓泽淼 on 2017/5/11.
//  Copyright © 2017年 DZM. All rights reserved.
//

/// 小说FuncView支持类型
enum DZMRMFuncViewType:NSInteger {
    case effect
    case font
    case fontSize
}

import UIKit

class DZMRMFuncView: DZMRMBaseView {
    
    /// 标题
    private var title = ""
    
    /// 标签数组
    private var labels = [String]()
    
    /// 选中索引
    private var selectIndex = 0
    
    /// 类型
    private var funcType = DZMRMFuncViewType.font
    
    /// 选中按钮
    private var selectButton:UIButton?
    
    /// titleLabel
    private lazy var labelTitle = UILabel()
    
    /// 字体大小增减间隙
    private let fontSpace = 1
    
    private lazy var labelFontSize = UILabel() //字体大小
    
    /// 初始化方法
    convenience init(frame:CGRect,readMenu:DZMReadMenu,funcType:DZMRMFuncViewType,title:String) {
        self.init(frame:frame,readMenu:readMenu,funcType:funcType,title:title,labels:[],selectIndex:0)
    }
    
    /// 初始化方法
    init(frame:CGRect,readMenu:DZMReadMenu,funcType:DZMRMFuncViewType,title:String,labels:[String],selectIndex:Int) {
        
        self.funcType = funcType
        
        self.title = title
        
        self.labels = labels
        
        self.selectIndex = selectIndex
        
        super.init(frame: frame, readMenu: readMenu)
    }
    
    override func addSubviews() {
        
        // 标题
        self.labelTitle.textColor = UIColor.white
        self.labelTitle.font = UIFont.system._12
        self.labelTitle.text = title
        self.addSubview(self.labelTitle)
        self.labelTitle.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(25.0)
            make.width.equalTo(55.0)
            make.centerY.equalToSuperview()
        }
        self.labelTitle.frame = CGRect(x: CGFloat.readerSpace._2, y: 0, width: 55, height: height)
        
        // 布局
        if self.funcType == .font || self.funcType == .effect {
            
            var preView:UIView?
            
            for index in 0 ..< self.labels.count {
                
                let label = self.labels[index]
                
                let button = UIButton(type: .custom)
                
                button.tag = index
                button.titleLabel?.font = UIFont.system._12
                if index == 0 {
                    button.contentHorizontalAlignment = .left
                } else if index == self.labels.count - 1 {
                    button.contentHorizontalAlignment = .right
                } else {
                    button.contentHorizontalAlignment = .center
                }
                button.setTitle(label, for: .normal)
                button.setTitleColor(UIColor.white, for: .normal)
                button.setTitleColor(UIColor.cj.red, for: .selected)
                button.addTarget(self, action: #selector(self.clickButton(button:)), for: .touchUpInside)
                self.addSubview(button)
                
                button.snp.makeConstraints({ (make) in
                    if let preaView_ = preView {
                        make.left.equalTo(preaView_.snp.right)
                        make.width.equalTo(preaView_.snp.width)
                    } else {
                        make.left.equalTo(self.labelTitle.snp.right).offset(50.0)
                    }
                    make.top.bottom.equalToSuperview()
                    if index >= self.labels.count - 1 {
                        make.right.equalToSuperview().offset(-25.0)
                    }
                })
                
                if index == selectIndex {
                    self.selectButton(button: button)
                }
                
                preView = button
            }
            
        } else { // 字体大小
            // left
            let leftButton = UIButton(type: .custom)
            leftButton.tag = 0
            leftButton.contentHorizontalAlignment = .left
            let leftButtonImage = UIImage(named: "menu_fontsize_minus", in: Bundle(for: type(of: self)), compatibleWith: nil)
            leftButton.setImage(leftButtonImage, for: .normal)
            leftButton.addTarget(self, action: #selector(self.clickFontSize(button:)), for: .touchUpInside)
            addSubview(leftButton)
            leftButton.snp.makeConstraints({ (make) in
                make.top.bottom.equalToSuperview()
                make.width.equalTo(44.0)
                make.left.equalTo(self.labelTitle.snp.right).offset(50.0)
            })
            
            // right
            let rightButton = UIButton(type: .custom)
            rightButton.tag = 1
            rightButton.contentHorizontalAlignment = .right
            let rightButtonImage = UIImage(named: "menu_fontsize_plus", in: Bundle(for: type(of: self)), compatibleWith: nil)
            rightButton.setImage(rightButtonImage, for: .normal)
            rightButton.addTarget(self, action: #selector(self.clickFontSize(button:)), for: .touchUpInside)
            addSubview(rightButton)
            rightButton.snp.makeConstraints({ (make) in
                make.top.bottom.equalToSuperview()
                make.right.equalToSuperview().offset(-25.0)
                make.width.equalTo(44.0)
            })
            
            self.labelFontSize.textAlignment = .center
            self.labelFontSize.text = "\(CLReadConfigure.shared.fontSize)"
            self.labelFontSize.textColor = UIColor.white
            self.labelFontSize.font = UIFont.system._12
            self.addSubview(self.labelFontSize)
            self.labelFontSize.snp.makeConstraints({ (make) in
                make.left.equalTo(leftButton.snp.right)
                make.right.equalTo(rightButton.snp.left)
                make.centerY.equalToSuperview()
            })
        }
    }
    
    /// .fontSize
    @objc private func clickFontSize(button:UIButton) {
        guard let readMenu_ = self.readMenu else { return }

        let config = CLReadConfigure.shared

        if button.tag == 0 { // left
            // 没有小于最小字体
            if (config.fontSize - fontSpace) >= config.minFontSize {
                
                config.fontSize -= fontSpace
                
                self.readMenu?.delegate?.readMenuClickSetuptFontSize?(readMenu: readMenu_, fontSize: CGFloat(config.fontSize))
            }
        } else { // right
            
            // 没有大于最大字体
            if (config.fontSize + fontSpace) <= config.maxFontSize {
                
                config.fontSize += fontSpace
                
                self.readMenu?.delegate?.readMenuClickSetuptFontSize?(readMenu: readMenu_, fontSize: CGFloat(config.fontSize))
            }
        }
        
        self.labelFontSize.text = "\(CLReadConfigure.shared.fontSize)"
    }
    
    /// .font .effect
    @objc private func clickButton(button:UIButton) {
        guard let readMenu_ = self.readMenu else { return }

        if button.isSelected {return}
        
        // 选中按钮
        self.selectButton(button: button)
        
        // 判断
        if funcType == .font {
            self.readMenu?.delegate?.readMenuClickSetuptFont?(readMenu: readMenu_, index: button.tag)
        }else{
            self.readMenu?.delegate?.readMenuClickSetuptEffect?(readMenu: readMenu_, index: button.tag)
        }
    }
    
    /// 选中按钮
    private func selectButton(button:UIButton) {
        if button.isSelected {return}
        selectButton?.isSelected = false
        button.isSelected = true
        self.selectButton = button
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
